#define _CRT_SECURE_NO_WARNINGS 1
#include "date.h"

ostream& operator<<(ostream& out, Date& d) {
	out << d.year_ << "/" << d.month_ << "/" << d.day_;
	return out;
}

bool Date::operator<(const Date& d) const {
	if (year_ < d.year_) {
		return true;
	}
	else if (year_ == d.year_ && month_ < d.month_) {
		return true;
	}
	else if (year_ == d.year_ && month_ == d.month_ && day_ < d.day_) {
		return true;
	}
	else
		return false;
}

bool Date::operator==(const Date& d) const {
	return year_ == d.year_ 
		&& month_ == d.month_ 
		&& day_ == d.day_;
}

bool Date::operator<=(const Date& d) const {
	return *this < d || *this == d;
}

bool Date::operator>(const Date& d) const{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d) const {
	return !(*this < d);
}

bool Date::operator!=(const Date& d) const {
	return !(*this == d);
}

int Date::GetDay() const {
	static int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month_ == 2 && ((year_ % 4 == 0 && year_ % 100 != 0) || year_ % 400 == 0)) {
		return 29;
	}
	return arr[month_ - 1];
}

Date& Date::operator=(const Date& d) {
	year_ = d.year_;
	month_ = d.month_;
	day_ = d.day_;
	return *this;
}

Date& Date::operator+=(int d) {
	day_ += d;
	while (day_ > GetDay()) {
		day_ -= GetDay();
		month_++;
		if (month_ == 13) {
			month_ = 1;
			year_++;
		}
	}
	return *this;
}

Date Date::operator+(int d) {
	Date dd(*this);
	return dd += d;
}

Date& Date::operator++() {
	return *this += 1;
}

Date Date::operator++(int) {
	Date cpy(*this);
	*this += 1;
	return cpy;
}

int Date::operator-(const Date& d) const {
	Date min, max;
	int count = 0;
	if (*this < d) {
		min = *this;
		max = d;
	}
	else {
		min = d;
		max = *this;
	}
	while (min != max) {
		min++;
		count++;
	}
	return count;
}

Date& Date::operator-=(int d) {
	day_ -= d;
	while (day_ <= 0) {
		month_--;
		if (month_ == 0) {
			month_ = 12;
			year_--;
		}
		day_ += GetDay();
	}
	return *this;
}

Date Date::operator-(int d) {
	Date cpy(*this);
	return cpy -= d;
}

Date& Date::operator--() {
	return *this -= 1;
}

Date Date::operator--(int) {
	Date cpy(*this);
	*this -= 1;
	return cpy;
}